<?php
// 后台用户模块
class CommodityLimitAction extends CommonAction {
	
	function _filter(&$map){
		// $map['id'] = array('egt',2);
		// $map['account'] = array('like',"%".$_POST['account']."%");
	}
	
	protected function _search($name = '') {
		//生成查询条件
        $name = $this->getActionName();
		$model = D ( $name );
		$map = array ();
		foreach ( $model->getDbFields () as $key => $val ) {
			if (isset ( $_REQUEST [$val] ) && $_REQUEST [$val] != '') {
				$map [$val] = $_REQUEST [$val];
			}
		}
		return $map;
	}
	
	public function _before_index(){
		$commodityInfo = D('CommodityInfo');
		$commodityInfo->ini();
		$commoditys = $commodityInfo->commodity_info();
		// dump($commoditys);
		$this->assign('commoditys',$commoditys);
	}
	
	// 插入数据
	public function insert() {
		// dump($_REQUEST);exit;
		$commodityInfo = D('CommodityInfo');
		$commodityInfo->ini();
		$commodity = $commodityInfo->commodity_info($_REQUEST['commodity_id']);
		if(empty($commodity) || $commodity['is_onsale']==false){
			echo "商品没有上架！";exit;
		}
		$data['commodity_id'] = $_REQUEST['commodity_id'];
		$data['limit_type'] = $_REQUEST['limit_type'];
		$data['limit_num'] = $_REQUEST['limit_num'];
		$data['left_num'] = $_REQUEST['limit_num'];
		$data['status'] = 1;
		$commodityLimit = D('CommodityLimit');
		$res = $commodityLimit->data($data)->add();
		if($res==false){
			echo "新增失败";exit;
		}
		// 后台操作日志记录
		$recordData['commodity_id'] = $_REQUEST['commodity_id'];
		$recordData['record_type'] = 1;//原始数据
		$recordData['change_num'] = $_REQUEST['limit_num'];
		$recordData['memo'] = $_REQUEST['memo'];
		$commodityLimitRecord = D('CommodityLimitRecord');
		$commodityLimitRecord->data($recordData)->add();
		$this->success('商品添加成功！');
	}
	
	// function edit() {
		// $name=$this->getActionName();
		// $model = M ( $name );
		// $id = $_REQUEST [$model->getPk ()];
		// $vo = $model->where("id='".$id."'")->find();
		// $this->assign ( 'vo', $vo );
		// $this->display ();
	// }
	
	public function update() {
		$id = $_REQUEST['id'];
		$data['commodity_id'] = $_REQUEST['commodity_id'];
		// $data['status'] = $_REQUEST['limit_status'];
		$data['left_num'] = num_f(num_f($_REQUEST['left_num'])+num_f($_REQUEST['change_num']));
		
		if($data['left_num']<0){
			echo "数量减至小于0";exit;
		}
		$commodityLimit = D('CommodityLimit');
		
		$commodityLimit->where("id = ".$id)->save($data);
		
		$recordData['commodity_id'] = $_REQUEST['commodity_id'];
		$recordData['record_type'] = 2;//原始数据
		$recordData['change_num'] = num_f($_REQUEST['change_num']);
		$recordData['memo'] = $_REQUEST['memo'];
		$commodityLimitRecord = D('CommodityLimitRecord');
		$commodityLimitRecord->data($recordData)->add();
		
		$this->success('修改成功！');
	}
	
	// public function 
	
}
?>